﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebComponents;
using WebComponents.Data;
using SiteUtils;
using System.Collections.Generic;

public partial class Products : System.Web.UI.UserControl {
  private string currentPage = "";
  protected void Page_Load(object sender, EventArgs e) {
    rptProducts.ItemDataBound += new RepeaterItemEventHandler(rptProducts_ItemDataBound);
    int id = ConvertType.ToInt(Request.Params["cid"]);
    string keyWord = ConvertType.ToString(Request.Params["k"]);
    List<ProductInfo> list = new List<ProductInfo>();
    if (keyWord != "") {
      list = ProductController.Search("vi-VN", keyWord);
    } else if (id == 0) {
      list = ProductController.List();
    } else
      list = ProductController.GetByCulture("vi-VN", id);
    CollectionPager pagerProduct = new CollectionPager();
    pagerProduct.MaxPages = 100;
    pagerProduct.PageSize = Global.PageSize("ProductsPageSize");
    pagerProduct.DataSource = list;
    int page = ConvertType.ToInt(Request.Params["p"]);
    if (page > 0)
      page -= 1;
    pagerProduct.DataSourcePaged.CurrentPageIndex = page;
    pagerProduct.BindToControl = rptProducts;
    rptProducts.DataSource = pagerProduct.DataSourcePaged;
    rptProducts.DataBind();

    string url = Request.Url.LocalPath;
    string currentPage = url.Substring(url.LastIndexOf("/"));
    if (url.Substring(url.LastIndexOf("/") + 1).StartsWith("p-")) {
      url = url.Substring(0, url.LastIndexOf("/"));
    } else
      url = url.Replace("-.aspx", "");
    //string cate = "";
    //string cateName = "";
    //if (id > 0) {
    //  cate = "cid=" + id + "&";
    //  CategoryInfo _cate = CategoryController.GetByCategoryID(id);
    //  if (_cate != null)
    //    cateName = ConvertType.StripDiacritics(_cate.NameVi);
    //}
    if (pagerProduct.PageCount > 1) {
      lblPager.Text = "";
      lblPager.Text += string.Format("<a href=\"{0}/p-{1}-.aspx\" class='linkpage'><<</a>", url, 1);
      for (int i = 0; i < pagerProduct.PageCount; i++) {
        if (i == page)
          lblPager.Text += string.Format("<a href=\"{1}/p-{0}-.aspx\" class='linkpage acitve'>{0}</a>", i + 1, url);
        else
          lblPager.Text += string.Format("<a href=\"{1}/p-{0}-.aspx\" class='linkpage'>{0}</a>", i + 1, url);
      }
      lblPager.Text += string.Format("<a href=\"{1}/p-{0}-.aspx\" class='linkpage'>>></a>", pagerProduct.PageCount, url);
    } else
      lblPager.Visible = false;
  }

  void rptProducts_ItemDataBound(object sender, RepeaterItemEventArgs e) {
    ProductInfo p = e.Item.DataItem as ProductInfo;
    if (p != null) {
      Image img = e.Item.FindControl("imgDescription") as Image;
      Repeater rptNhomGia = e.Item.FindControl("rptNhomGia") as Repeater;
      if (p.ProductID > 0) {
        img.ImageUrl = "~/" + p.SmallImageUrl;
        var list = ProductAttributesController.GetByProductID(p.ProductID);
        //if (list.Count > 0) {
        rptNhomGia.DataSource = list;
        rptNhomGia.DataBind();
        //}// else
        //rptNhomGia.Visible = false;
      } else
        img.Visible = false;
    }
  }
}
